package tr.mesir.view;

import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import tr.mesir.domain.AddressBook;
import tr.mesir.domain.Contact;
import tr.mesir.service.AddressBookService;

/**
 * This the backing-bean used for populating data to demonstrate the application.
 * 
 * @author mertcaliskan
 *
 */
@Controller("dataPopulatorController")
@Scope("request")
public class DataPopulatorController extends PageCodeBase {

	@Autowired
	private AddressBookService addressBookService;
	
	public String populateData() {
		AddressBook abWork = createAddressBook("Work");
		abWork.getContacts().add(createContact("John Doe", "john@doe.com", "555-1111"));
		abWork.getContacts().add(createContact("Robert De Niro", "robert@deniro.com", "555-2222"));
		abWork.getContacts().add(createContact("Al Pacino", "al@pacino.com", "555-3333"));
		
		AddressBook abPersonal = createAddressBook("Personal");
		abPersonal.getContacts().add(createContact("Johnny Depp", "johnny@depp.com", "555-4444"));
		abPersonal.getContacts().add(createContact("Jack Nicholson", "jack@nicholson.com", "555-5555"));
		
		addressBookService.save(abWork);
		addressBookService.save(abPersonal);
		
		return "addressBooks";
	}

	private Contact createContact(String name, String email, String phone) {
		Contact contact = new Contact();
		contact.setName(name);
		contact.setEmail(email);
		contact.setPhone(phone);
		
		return contact;
	}

	private AddressBook createAddressBook(String title) {
		AddressBook addressBook = new AddressBook();
		addressBook.setTitle(title);
		addressBook.setCreationDate(new DateTime());
		
		return addressBook;
	}
}